﻿using AuthenticationAPI.Models.DbContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace AuthenticationAPI.Models.Utility
{
    public class CorporationHelper
    {
        public static List<corporation> GetAll()
        {
            using (var db = new AuthenticationEntities())
            {
                var list = from p in db.Corporation
                           select new corporation()
                           {
                               name = p.CorporationName,
                               shortName = p.Remark,
                               departments = db.Department.Where(q => q.CorpId == p.UID).ToList().Select(q => new department()
                               {
                                   name = q.DepartmentName,
                               }).ToList()
                           };
                var result = list.ToList();
                result.ForEach(p => p.departments.ForEach(t => t.admins = CorporationHelper.GetReviewer(p.shortName, t.name)));
                return result;
            }
        }


        public static List<string> GetReviewer(string corp, string depart)
        {
            using (var db = new AuthenticationEntities())
            {
                var corpReviewers = db.User.Where(t => t.Corporation == corp && (t.Authentication == "CorpAdministrator" ||
                                      t.Authentication == "Owner")).Select(t => t.RealName).ToList();

                var departReviewers = db.User.Where(t => t.Corporation == corp && t.Department == depart && t.Authentication == "DepartAdministrator")
                                        .Select(t => t.RealName).ToList();
                corpReviewers.AddRange(departReviewers);
                return corpReviewers;
            }
        }
    }
}